package cn.edu.ujn.finallab.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.ujn.finallab.dao.Orders;
import cn.edu.ujn.finallab.dao.Orders;
import cn.edu.ujn.finallab.dao.OrdersMapper;
import cn.edu.ujn.finallab.dao.Room;
import cn.edu.ujn.finallab.util.Page;
@Service
public class OrderServiceImpl implements IOrderService {

	@Autowired
	private OrdersMapper ordersMapper;
	
	@Override
	public List<Orders> selectAllOrder() {
		// TODO Auto-generated method stub
		List<Orders> allOrder = this.ordersMapper.selectAllOrder();
		return allOrder;
	}

	@Override
	public List<Orders> selectByMany(String number, String name, String intime, String outtime) {
		// TODO Auto-generated method stub
		List<Orders> selectByMany = this.ordersMapper.selectByMany(number, name, intime, outtime);
		return selectByMany;
	}

	@Override
	public int insertOrder(Orders orders) {
		// TODO Auto-generated method stub
		int insert = this.ordersMapper.insert(orders);
		return insert;
	}

	@Override
	public int deleteOrder(int id) {
		// TODO Auto-generated method stub
		int i = this.ordersMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	public int updateOrder(Orders orders) {
		// TODO Auto-generated method stub
		int i = this.ordersMapper.updateByPrimaryKey(orders);
		return i;
	}

	@Override
	public Page<Orders> selectByPage(int currentPage) {
		// TODO Auto-generated method stub
		HashMap<String, Object> map = new HashMap<String, Object>();
		Page<Orders> page = new Page<Orders>();
		// 封装当前页数
		page.setPage(currentPage);
		// 每页显示的数据
		int pageSize = 8;
		page.setSize(pageSize);
		// 封装总记录数
		int totalCount = ordersMapper.selectOrderCount();
		System.out.println("总的记录数为" + totalCount);
		// 获取记录总数
		page.setTotal(totalCount);
		// 封装总页数
		double totalcount = totalCount;
		double num = Math.ceil(totalcount / pageSize);
		page.setTotalpage((int)num);
		// 起始的记录值
		map.put("start", (currentPage - 1) * pageSize);
		// 获取每个页面的pageSize
		map.put("size", page.getSize());
		List<Orders> list = ordersMapper.selectByPageLimit(map);
		page.setRows(list);
		// 返回一个page
		return page;
	}
	
	//根据顾客身份证号查询未入住订单
	@Override
	public List<Orders> selectByIdcard(String idcard) {
		return ordersMapper.selectUnfinishedOrderByIdcard(idcard);
	}
	
	//根据订单号查询订单
	@Override
	public Orders selectByOrderNumber(String ordernumber) {
		return ordersMapper.selectByOrderNumber(ordernumber);
	}

	@Override
	public Orders selectByRoomNumberAndState(String roomnumber, String state) {
		// TODO Auto-generated method stub
		Orders orders = this.ordersMapper.selectByRoomNumberAndState(roomnumber, state);
		return orders;
	}

	@Override
	public Page<Orders> selectByPageOfMany(int currentPage, String number, String name, String intime, String outtime) {
		// TODO Auto-generated method stub
		HashMap<String, Object> map = new HashMap<String, Object>();
		Page<Orders> page = new Page<Orders>();
		page.setPage(currentPage);
		int pageSize = 10;
		page.setSize(pageSize);
		//封装总记录数
		int totalCount = this.ordersMapper.selectOrderCountOfMany(number, name, intime, outtime);
		//封装记录总数
		page.setTotal(totalCount);
		//封装总页数
		double totalcount = totalCount;
		double num = Math.ceil(totalcount / pageSize);
		page.setTotalpage((int)num);
		// 起始的记录值
		map.put("start", (currentPage - 1) * pageSize);
		// 获取每个页面的pageSize
		map.put("size", page.getSize());
		// 封装客房类型
		//封装条件
		map.put("number", number);
		map.put("name", name);
		map.put("intime", intime);
		map.put("outtime", outtime);
		List<Orders> list = this.ordersMapper.selectByPageLimitOfMany(map);
		page.setRows(list);
		//返回一个page
		return page;
	}

}
